/**
 * 
 */
package org.mips.Sirius.provider.SIRI;

import java.io.StringReader;
import java.util.ArrayList;

import org.mips.Sirius.components.Vehicle;
import org.mips.Sirius.provider.ProviderException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

/**
 * @author mips
 * 
 */
public class SIRIVehicleMonitoringDelivery {

	ArrayList<Vehicle> vehicles;

	public SIRIVehicleMonitoringDelivery(String xml) throws ProviderException {

		parse(xml);
	}

	public ArrayList<Vehicle> getVehicles() {
		return vehicles;
	}

	private void parse(String xml) throws ProviderException {

		vehicles = new ArrayList<Vehicle>();

		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser xpp = factory.newPullParser();

			xpp.setInput(new StringReader(xml));
			int eventType = xpp.getEventType();

			String lineRef = null;
			String lineName = null;
			int latitude = 0, longitude = 0;
			double bearing = 0;
			boolean monitored = false;

			boolean openLineRef = false;
			boolean openLongitude = false;
			boolean openLatitude = false;
			boolean openBearing = false;
			boolean openPublishedLineName = false;
			boolean openMonitored = false;

			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {
					if (xpp.getName().equals("Longitude")) {
						openLongitude = true;
					} else if (xpp.getName().equals("Latitude")) {
						openLatitude = true;
					} else if (xpp.getName().equals("Bearing")) {
						openBearing = true;
					} else if (xpp.getName().equals("PublishedLineName")) {
						openPublishedLineName = true;
					} else if (xpp.getName().equals("Monitored")) {
						openMonitored = true;
					} else if (xpp.getName().equals("LineRef")) {
						openLineRef = true;
					}
				} else if (eventType == XmlPullParser.END_TAG) {
					if (xpp.getName().equals("MonitoredVehicleJourney")
							&& monitored) {
						vehicles.add(new Vehicle(lineRef, lineName,
								latitude, longitude, bearing));
					} else if (xpp.getName().equals("Longitude")) {
						openLongitude = false;
					} else if (xpp.getName().equals("Latitude")) {
						openLatitude = false;
					} else if (xpp.getName().equals("Bearing")) {
						openBearing = false;
					} else if (xpp.getName().equals("PublishedLineName")) {
						openPublishedLineName = false;
					} else if (xpp.getName().equals("Monitored")) {
						openMonitored = false;
					} else if (xpp.getName().equals("LineRef")) {
						openLineRef = false;
					}
				} else if (eventType == XmlPullParser.TEXT) {
					if (openPublishedLineName) {
						lineName = xpp.getText();
					} else if (openLatitude) {
						latitude = Integer.valueOf(xpp.getText());
					} else if (openLongitude) {
						longitude = Integer.valueOf(xpp.getText());
					} else if (openBearing) {
						bearing = Double.valueOf(xpp.getText());
					} else if (openLineRef) {
						lineRef = xpp.getText();
					} else if (openMonitored) {
						if (xpp.getText().equals("true")) {
							monitored = true;
						} else {
							monitored = false;
						}
					}
				}
				eventType = xpp.next();
			}
		} catch (Exception e) {
			throw new ProviderException(e,
					"Couldn't parse VehicleMonitoringDelivery");
		}

		Log.d("debug", "Found " + vehicles.size() + " monitored vehicles: "
				+ vehicles);
	}
}
